package com.example.todo;

import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FilteredResultsActivity extends Activity {

	DBAdapter myDb;
	public final static String EXTRA_TASK_NAME = "com.example.ToDo.MESSAGE";
	public final static String EXTRA_CATEGORY = "com.example.ToDo.CATEGORY";
	public final static String EXTRA_SUBJECT = "com.example.ToDo.SUBJECT";
	public final static String EXTRA_PROJECT = "com.example.ToDo.PROJECT";
	public final static String EXTRA_DATE = "com.example.ToDo.DATE";
	public final static String EXTRA_LOCATION = "com.example.ToDo.LOCATION";
	public final static String EXTRA_DESCRIPTION = "com.example.ToDo.DESCRIPTION";
	public final static String EXTRA_ID = "com.example.ToDo.ID";
	public final static String TEST_CATEGORY = "School";
	public static String FILTER_BY_CATEGORY;
	public static String FILTER_BY_NAME;
	public static String FILTER_BY_SUBJECT;
	public static String FILTER_BY_PROJECT;
	public static String FILTER_BY_DUEDATE;
	public static String FILTER_BY_LOCATION;
	public static String NAV_CATEGORY;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_filtered_results);
		Intent intent = getIntent();
		NAV_CATEGORY = intent.getStringExtra(MainActivity.EXTRA_FILTER_CATEGORY); //THIS IS FOR THE NAV FILTER
		FILTER_BY_CATEGORY = intent.getStringExtra(FilterActivity.EXTRA_FILTER_CATEGORY);
		FILTER_BY_NAME = intent.getStringExtra(FilterActivity.EXTRA_FILTER_NAME);
		FILTER_BY_SUBJECT = intent.getStringExtra(FilterActivity.EXTRA_FILTER_SUBJECT);
		FILTER_BY_PROJECT = intent.getStringExtra(FilterActivity.EXTRA_FILTER_PROJECT);
		FILTER_BY_DUEDATE = intent.getStringExtra(FilterActivity.EXTRA_FILTER_DUEDATE);
		FILTER_BY_LOCATION = intent.getStringExtra(FilterActivity.EXTRA_FILTER_Location);
		openDB();
	}

// START NAVIGATION MENU CODE
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.filtered_results, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(
					R.layout.fragment_filtered_results, container, false);
			return rootView;
		}
	}
// END NAVIGATION MENU CODE
	
	protected void onDestroy() {
		super.onDestroy();
		closeDB();
	}
	private void openDB() {
		myDb = new DBAdapter(this);
		myDb.open();
	}
	private void closeDB() {
		myDb.close();
	}
	
	//Filter the results
	public void onClickShowResults(View view){
		myDb.open();
		Cursor cursor = null;
		myDb.open();
		// SEE IF THE INTENT CAME FROM THE NAVIGATION MENU
		if(NAV_CATEGORY != null){
			cursor = myDb.getRow_Category(NAV_CATEGORY); //THIS WORKS WITH THE NAV FILTER MENU			
		}
		// THE INTENT CAME FROM THE FILTER TASK ACTIVITY
		else{
			String query = "SELECT * FROM mainTable WHERE ";
			if (FILTER_BY_CATEGORY.length() > 3){
				query += ("category = '"+FILTER_BY_CATEGORY+"'");
			}
			if (FILTER_BY_NAME.length() > 1){
				if (query.length() > 31){
					query += (" AND name ='" +FILTER_BY_NAME+"'");
				}
				else{
					query += ("name ='" +FILTER_BY_NAME+"'");
				}	
			}
			if (FILTER_BY_SUBJECT.length() > 1){
				if (query.length() > 31){
					query += (" AND subject ='" +FILTER_BY_SUBJECT+"'");
				}
				else{
					query += ("subject ='" +FILTER_BY_SUBJECT+"'");
				}
			}
			if (FILTER_BY_PROJECT.length() > 1){
				if (query.length() > 31){
					query += (" AND project ='" +FILTER_BY_PROJECT+"'");
				}
				else{
					query += ("project ='" +FILTER_BY_PROJECT+"'");
				}
			}
			if (FILTER_BY_DUEDATE.length() > 1){
				if (query.length() > 31){
					query += (" AND date ='" +FILTER_BY_DUEDATE+"'");
				}
				else{
					query += ("date ='" +FILTER_BY_DUEDATE+"'");
				}
			}
			if (FILTER_BY_LOCATION.length() > 1){
				if (query.length() > 31){
					query += (" AND location ='" +FILTER_BY_LOCATION+"'");
				}
				else{
					query += ("location ='" +FILTER_BY_LOCATION+"'");
				}
			}
			cursor = myDb.getRow_Filter(query);
		}
		
        displayRecordSet(cursor);
		myDb.close();
	}
	
	
	private void displayRecordSet(Cursor cursor) {
		String message = "";
		LinearLayout buttonContainer = (LinearLayout) findViewById(R.id.button_layout_filter);
	    buttonContainer.removeAllViews();
		//Populate the message from the cursor
		//Reset cursor to start, checking to see if there's data:
		if (cursor.moveToFirst()){
			do{
				//Process data:
				final int id = cursor.getInt(DBAdapter.COL_ROWID);
				final String task_id = Integer.toString(id);
				final String  task_name = cursor.getString(DBAdapter.COL_TASK_NAME);
				final String category = cursor.getString(DBAdapter.COL_CATEGORY);
				final String subject = cursor.getString(DBAdapter.COL_SUBJECT);
				final String project = cursor.getString(DBAdapter.COL_PROJECT);
				final String date = cursor.getString(DBAdapter.COL_DATE);
				final String description = cursor.getString(DBAdapter.COL_DESCRIPTION);
				final String location = cursor.getString(DBAdapter.COL_LOCATION);
				//Append data to the message:
				message += "id= " + id +
						", task=" +task_name +
						", category=" + category +
						", subject=" + subject +
						", project=" + project +
						", date=" + date +
						", description=" + description +
						", location=" + location +
						"\n";	
			Button button = new Button(this);
			button.setText(task_name);
			button.setOnClickListener(new View.OnClickListener() {
			    @Override
			    public void onClick(View v) {	    	
			    	Intent intent = new Intent(FilteredResultsActivity.this, TaskDetailsActivity.class);
			    	intent.putExtra(EXTRA_ID,  task_id);
			    	intent.putExtra(EXTRA_TASK_NAME,  task_name);
			    	intent.putExtra(EXTRA_CATEGORY,  category);
			    	intent.putExtra(EXTRA_SUBJECT,  subject);
			    	intent.putExtra(EXTRA_PROJECT,  project);
			    	intent.putExtra(EXTRA_DATE,  date);
			    	intent.putExtra(EXTRA_LOCATION,  location);
			    	intent.putExtra(EXTRA_DESCRIPTION,  description);
			    	startActivity(intent);
			    }
			});
			LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
			
			buttonContainer.addView(button, lp);
			} while(cursor.moveToNext());
		}
		//Close cursor to avoid resource leak.
		cursor.close();
	}	
}
